package com.ss.ttm.player;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.util.SparseArray;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;

public final class m {
    static final class a extends c {
        int a;

        public a(int arg1) {

            this.a = arg1;
        }
    }

    static final class b extends c {
        String a;

        public b(String arg1) {

            this.a = arg1;
        }
    }

    static class c {
        protected c() {

        }
    }

    private static String a;
    private static String b;
    private static String c;
    private static boolean d;
    private static final SparseArray e = new SparseArray();

    static {
        m.b = "libttmplayer.so";
        m.c = "ttplayer_crash.log";
        m.d = false;
        m.b(13, 1);
        m.b(14, "1.0.1");
        m.b(15, "version code:1,name:1.0.1default sdk info 2016-12-05");
        m.a(0, false);
        m.a(1, true);
        m.a(2, true);
        m.a(3, false);
        m.a(4, false);
        m.a(5, false);
        m.a(6, false);
        m.a(7, false);
        m.a(8, false);
        m.b(10, 0);
        m.a(11, false);
        m.b(18, 2);
        m.g();
    }

    public static final boolean a(int arg4) {
        boolean v0_1;
        Object v0 = m.e.get(arg4);
        if (v0 == null || !(v0 instanceof a)) {
            v0_1 = false;
        } else if (((a) v0).a == 1) {
            v0_1 = true;
        } else {
            v0_1 = false;
        }

        return v0_1;
    }

    public static final int a(int arg2, int arg3) {
        Object v0 = m.e.get(arg2);
        if (v0 != null && ((v0 instanceof a))) {
            arg3 = ((a) v0).a;
        }

        return arg3;
    }

    public static final String a(int arg2, String arg3) {
        if (arg2 == 17) {
            arg3 = m.c;
        } else {
            Object v0 = m.e.get(arg2);
            if (v0 != null && ((v0 instanceof b))) {
                arg3 = ((b) v0).a;
            }
        }

        return arg3;
    }

    public static final void a(int arg1, boolean arg2) {
        int v0 = arg2 ? 1 : 0;
        m.b(arg1, v0);
    }

    public static final String a(Context arg3) {
        if (arg3 != null && m.a == null) {
            m.a = arg3.getApplicationContext().getFilesDir().getAbsolutePath();
        }

        if (m.a == null) {
            m.a = m.e(arg3);
        }

        if (m.d) {
            Log.i("ttplayer", "<TTPlayerConfigure.java,getAppCrashFilePath2,211>app files path:" + m.a);
        }

        String v0 = m.a != null ? m.a + File.separatorChar + m.c : null;
        return v0;
    }

    public static final boolean a() {
        boolean v0 = true;
        if (m.a(6, 0) != 1) {
            if (m.a(1, 0) == 0) {
                v0 = false;
            } else {
                if (m.a(7, 0) != 1 && m.a(8, 0) != 1) {
                    if (m.a(10, 0) >= 3) {
                        v0 = false;
                    } else {
                    }

                    return v0;
                }

                v0 = false;
            }
        }

        return v0;
    }

    private static boolean a(String arg7, String arg8) {
        int v2_1;
        FileOutputStream v1_2;
        FileInputStream v3 = null;
        FileInputStream v2 = null;
        boolean v0 = false;
        File v4 = new File(arg8);
        if (!v4.exists()) {
            return v0;
        }

        if (!v4.isFile()) {
            return v0;
        }

        File v5 = new File(arg7);
        if (v5.exists()) {
            new File(arg7).delete();
        } else if (!v5.getParentFile().exists() && !v5.getParentFile().mkdirs()) {
            return v0;
        }

        try {
            v3 = new FileInputStream(v4);
            v1_2 = new FileOutputStream(v5);
            v2_1 = 1024;
            byte[] v2_4 = new byte[v2_1];
            while (true) {
                int v5_1 = ((InputStream) v3).read(v2_4);
                if (v5_1 == -1) {
                    break;
                }
                ((OutputStream) v1_2).write(v2_4, 0, v5_1);
            }

            ((OutputStream) v1_2).close();
            ((InputStream) v3).close();
            v4.delete();
            return true;
        } catch (FileNotFoundException v2_3) {
        }catch (IOException v2_2) {
        } finally {
            try {
                ((InputStream) v3).close();
            } catch (IOException v0_2) {
                v0_2.printStackTrace();
            }

            try {
                ((InputStream) v3).close();
            } catch (IOException v0_2) {
                v0_2.printStackTrace();
            }
        }
        return false;
    }

    public static final void b(int arg2, int arg3) {
        Object v0 = m.e.get(arg2);
        if (v0 == null) {
            m.e.append(arg2, new a(arg3));
        } else if ((v0 instanceof a)) {
            if (arg2 == 2 && (m.a(6))) {
                return;
            }
            ((a) v0).a = arg3;
        } else {
            m.e.remove(arg2);
            m.e.append(arg2, new a(arg3));
        }
    }

    public static final void b(int arg2, String arg3) {
        Object v0 = m.e.get(arg2);
        if (v0 == null) {
            m.e.append(arg2, new b(arg3));
        } else if ((v0 instanceof b)) {
            ((b) v0).a = arg3;
        } else {
            m.e.remove(arg2);
            m.e.append(arg2, new b(arg3));
        }
    }

    public static final String b() {
        StringBuilder v0 = new StringBuilder();
        v0.append(m.a).append(File.separatorChar);
        v0.append("plugins").append(File.separatorChar);
        v0.append("com.ss.ttm").append(File.separatorChar);
        v0.append("data").append(File.separatorChar);
        v0.append("files").append(File.separatorChar);
        v0.append(m.c);
        return v0.toString();
    }

    @Deprecated
    public static final String b(Context arg3) {
        if (arg3 != null && m.a == null) {
            m.a = arg3.getApplicationContext().getFilesDir().getAbsolutePath();
        }

        if (m.a == null) {
            m.a = m.e(arg3);
        }

        if (m.d) {
            Log.i("ttplayer", "<TTPlayerConfigure.java,getAppCrashFileStorePath,245>app files path:" + m.a);
        }

        String v0 = m.a != null ? m.a + File.separatorChar + m.c : null;
        return v0;
    }

    public static final String c() {
        StringBuilder v0 = new StringBuilder();
        v0.append(Environment.getExternalStorageDirectory());
        v0.append(File.separatorChar).append(m.c);
        return v0.toString();
    }

    @Deprecated
    public static final String c(Context arg4) {
        String v0 = null;
        if (!new File(m.b(arg4)).exists()) {
            StringBuilder v0_1 = new StringBuilder();
            v0_1.append(m.a).append(File.separatorChar);
            v0_1.append("plugins").append(File.separatorChar);
            v0_1.append("com.ss.ttm").append(File.separatorChar);
            v0_1.append("data").append(File.separatorChar);
            v0_1.append("files").append(File.separatorChar);
            v0_1.append(m.c);
            v0 = v0_1.toString();
        } else if (m.d) {
            Log.i("ttplayer", "<TTPlayerConfigure.java,getAppCrashFileStorePath,278>carsh file path:" + v0);
        }

        return v0;
    }

    public static boolean d() {
        return m.d;
    }

    public static final String d(Context arg2) {
        String v0 = null;
        if (arg2 != null && m.a == null) {
            m.a = arg2.getApplicationContext().getFilesDir().getAbsolutePath();
        }

        if (m.a != null) {
            return m.a;
        }

        if (arg2 != null) {
            try {
                m.a = arg2.getApplicationContext().getFilesDir().getAbsolutePath();
            } catch (Exception v1) {
                return v0;
            }

            v0 = m.a;
        }

        return v0;
    }

    public static void e() {
        String v0 = m.a + File.separatorChar + m.b;
        if (m.d) {
            Log.i("ttplayer", "<TTPlayerConfigure.java,checkDebugTTPlayerLib,346>lib dst path:" + v0);
        }

        File v1 = new File(Environment.getExternalStorageDirectory() + "/ttplayer/" + m.b);
        if (v1.exists()) {
            m.a(v0, v1.getPath());
        }
    }

    private static String e(Context arg2) {
        String v0 = null;
        if (m.a != null) {
            return m.a;
        }

        if (arg2 != null) {
            try {
                m.a = arg2.getApplicationContext().getFilesDir().getAbsolutePath();
            } catch (Exception v1) {
                return v0;
            }

            v0 = m.a;
        }

        return v0;
    }

    public static String f() {
        String v0_1;
        Context v0 = null;
        if (m.a == null) {
            m.a = m.e(v0);
        }

        if (m.a != null) {
            v0_1 = m.a + File.separatorChar + m.b;
        } else {
            v0_1 = null;
        }

        return v0_1;
    }

    private static final void g() {
        boolean v0_3;
        boolean v3;
        BufferedReader v5;
        FileReader v4;
        if (m.a(0, 0) != 0) {
            return;
        }

        m.b(0, 1);
        try {
            File v0_1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separatorChar + "ttplayer.debug");
            if (!v0_1.exists()) {
                return;
            }
            v4 = new FileReader(v0_1);
            v5 = new BufferedReader(((Reader) v4));
            v3 = false;
            while (true) {
                String v0_2 = v5.readLine();
                if (v0_2 == null) {
                    label_70:
                    v5.close();
                    v4.close();
                    m.a(6, v3);
                    return;
                }
                String v6 = v0_2.replace("\r\n", "");
                if (v6.contains(":on")) {
                    v0_3 = true;
                } else if (v6.contains(":off")) {
                    v0_3 = false;
                } else {
                    v0_3 = false;
                }
                try {
                    if (v6.startsWith("debug_player:")) {
                        v3 = v0_3;
                        continue;
                    }

                    if (v6.startsWith("ipc_player:")) {
                        m.a(2, v0_3);
                        continue;
                    }

                    if (v6.startsWith("print_info:")) {
                        m.d = v0_3;
                        continue;
                    }

                    if (v6.startsWith("check_lib:")) {
                        m.a(3, v0_3);
                        continue;
                    }

                    if (!v6.startsWith("throw_crash:")) {
                        continue;
                    }

                    m.a(4, v0_3);
                    continue;
                } catch (Throwable v0) {
                    v0.printStackTrace();
                    return;
                }
            }
        } catch (Throwable v0) {
        }
    }
}

